package com.higgins.eventtracking.client.finance;

import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockLayoutPanel.Direction;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.AssetTrackingService;
import com.higgins.eventtracking.client.AssetTrackingServiceAsync;
import com.higgins.eventtracking.client.FinanceTrackingService;
import com.higgins.eventtracking.client.FinanceTrackingServiceAsync;
import com.higgins.eventtracking.client.SecurityAccessDeniedException;
import com.higgins.eventtracking.client.dto.DynamicTypeDTO;
import com.higgins.eventtracking.client.dto.TransactionDTO;

public class FinanceAddTransaction extends VerticalPanel		{
	
	private static final String SERVER_ERROR = "An error occurred while "
		+ "attempting to contact the server. Please check your network "
		+ "connection and try again.";
	private final FinanceTrackingServiceAsync financeTrackingService = GWT
			.create(FinanceTrackingService.class);
	private final AssetTrackingServiceAsync assetTrackingService = GWT
			.create(AssetTrackingService.class);
	
	final CheckBox yearlyExpenseCheckBox = new CheckBox();
	
	private HorizontalPanel rowOne = new HorizontalPanel();
	private HorizontalPanel rowTwo = new HorizontalPanel();
	private HorizontalPanel rowThree = new HorizontalPanel();
	private HorizontalPanel rowFour = new HorizontalPanel();
	private HorizontalPanel rowFive = new HorizontalPanel();
	private HorizontalPanel rowSix = new HorizontalPanel();
	private HorizontalPanel rowSeven = new HorizontalPanel();
	private HorizontalPanel rowEight = new HorizontalPanel();
	private HorizontalPanel rowNine = new HorizontalPanel();
	private HorizontalPanel rowTen = new HorizontalPanel();
	private HorizontalPanel rowEleven = new HorizontalPanel();
	private HorizontalPanel rowTwelve = new HorizontalPanel();
	private HorizontalPanel rowThirteen = new HorizontalPanel();
	private HorizontalPanel rowFourteen = new HorizontalPanel();

	private HorizontalPanel rowFifteen = new HorizontalPanel();
	private HorizontalPanel rowSixteen = new HorizontalPanel();
	private HorizontalPanel rowSeventeen = new HorizontalPanel();
	private HorizontalPanel rowEighteen = new HorizontalPanel();
	private HorizontalPanel rowNineteen = new HorizontalPanel();

	
	final Label totalSpentThisWeekLabel = new Label();
	final Label totalSpentThisMonthLabel = new Label();
	final Label forecastSpendThisWeek = new Label();
	final Label forecastSpendThisMonth = new Label();
	final Label averageWeeklySpend = new Label();
	final Label forecastSavingThisMonth = new Label();
	final Label weekNumber = new Label();

	final Label totalSavingsLabel = new Label();
	final Label forecastSavingsAtEndOfNextQuarterLabel = new Label();
	final Label forecastUnemployedSavingsAtEndOfNextQuarterLabel = new Label();
	final Label endOfNextQuarterLabel = new Label();
	
	Label totalSpendThisWeekTitleLabel = new Label("Total Spend This Week: (Actual)");
	Label totalSavingsTitleLabel = new Label("Total Savings:");
	Label totalSpendThisMonthTitleLabel = new Label("Total Spend This Month: (Actual)");
	Label totalSavingsEndOfNextQuarterUnEmployedTitleLabel = new Label("Total Savings End of Next Quarter (Unemployed):");
	Label totalSpendThisWeekForecastTitleLabel = new Label("Total Spend This Week: (Forecast)");
	Label totalSavingsEndOfNextQuarterEmployed = new Label("Total Savings End of Next Quarter (Employed):");
	Label totalSpendThisMonthForecastTitleLabel = new Label("Total Spend This Month: (Forecast)");
	Label endOfNextQuarterTitleLabel = new Label("End of Next Quarter:");
	Label averageWeeklySpendTitleLabel = new Label("Average Weekly Spend:");
	Label forecastSavingsThisMonthTitleLabel = new Label("Forecast Saving This Month:");
	Label weekNumberTitleLabel = new Label("Week Number:");

	String columnOneWidth = "250px";
	String columnTwoWidth = "250px";
	String columnThreeWidth = "400px";
	
	String cellHeight = "2px";
	
	final Button sendButton = new Button("Add");
	final TextBox nameField = new TextBox();
	final ListBox transactionTypeListBox = new ListBox();
	final TextBox amountTextBox = new TextBox();
	final TextBox descriptionTextBox = new TextBox();
	final Label errorLabel = new Label();
	final DialogBox dialogBox = new DialogBox();
	final Button closeButton = new Button("Close");
	final Label textToServerLabel = new Label();
	final HTML serverResponseLabel = new HTML();
	private boolean firstRun = true;

	Label labelRowSixBlank = new Label();
	Label labelRowSevenBlank = new Label();
	Label labelRowEightBlank = new Label();
	Label labelRowNineBlank = new Label();
	Label labelRowTenBlank = new Label();
	Label labelRowElevenBlank = new Label();
	Label labelRowTwelveBlank = new Label();
	Label labelRowThirteenBlank = new Label();
	Label labelRowFourteenBlank = new Label();
	
	public FinanceAddTransaction()			{
	}
	
	public void update()	{
		if (firstRun)
			initialise();
		else
			refresh();
	}

	public void refresh()	{
		
	}
	public void initialise()	{
		firstRun = false;
		populatePageComponents();
		attachPageComponents();
		
		populateFinanceStatsFields();
		createDialogBox();
		
		createHandlers();
	}
		
	public void populateFinanceStatsFields()	{
		populateYearlyExpenseCheckBox();
		populateTotalSpentThisWeek();
		populateTotalSpentThisMonth();
		populateForecastSpendThisWeek();
		populateForecastSpendThisMonth();
		populateAverageWeeklySpend();
		populateForecastSavingsThisMonth();
		populateWeekNumber();
		
		populateTotalSavings();
		populateForecastForSavingsAtEndOfNextQuarter();
		populateForecastUnemployedForSavingsAtEndOfNextQuarter();
		populateEndOfNextQuarter();
	}
	
	private void populateYearlyExpenseCheckBox()	{
		yearlyExpenseCheckBox.setValue(false);
	}
	
	private void populateTotalSpentThisWeek() throws IllegalArgumentException	{
		try {
			financeTrackingService.retrieveTotalSpentThisWeek(new Date(), true,
					new AsyncCallback<Double>()	{

						@Override
						public void onFailure(Throwable caught) {
							totalSpentThisWeekLabel.setText("FAILURE");
						}

						@Override
						public void onSuccess(Double result) {
							totalSpentThisWeekLabel.setText(convertDoubleToString(result));
						}
					});
		} catch (SecurityAccessDeniedException e) {
			totalSpentThisWeekLabel.setText("FAILURE");
		}
	}
	private void populateTotalSpentThisMonth() throws IllegalArgumentException	{
		try {
			financeTrackingService.retrieveTotalSpentThisMonth(new Date(), true, 
					new AsyncCallback<Double>()	{

						@Override
						public void onFailure(Throwable caught) {
							totalSpentThisMonthLabel.setText("FAILURE");
						}

						@Override
						public void onSuccess(Double result) {
							totalSpentThisMonthLabel.setText(convertDoubleToString(result));
						}
					});
		} catch (SecurityAccessDeniedException e) {
			totalSpentThisMonthLabel.setText("FAILURE");
		}
	}
	private void populateForecastSpendThisWeek() throws IllegalArgumentException	{
		try {
			financeTrackingService.retrieveForecastSpendThisWeek(new Date(), true, 
					new AsyncCallback<Double>()	{

						@Override
						public void onFailure(Throwable caught) {
							forecastSpendThisWeek.setText("FAILURE");
						}

						@Override
						public void onSuccess(Double result) {
							forecastSpendThisWeek.setText(convertDoubleToString(result));
						}
					});
		} catch (SecurityAccessDeniedException e) {
			forecastSpendThisWeek.setText("FAILURE");
		}
	}
	private void populateForecastSpendThisMonth() throws IllegalArgumentException	{
		try {
			financeTrackingService.retrieveForecastSpendThisMonth(new Date(), true, 
					new AsyncCallback<Double>()	{

						@Override
						public void onFailure(Throwable caught) {
							forecastSpendThisMonth.setText("FAILURE");
						}

						@Override
						public void onSuccess(Double result) {
							forecastSpendThisMonth.setText(convertDoubleToString(result));
						}
					});
		} catch (SecurityAccessDeniedException e) {
			forecastSpendThisMonth.setText("FAILURE");
		}
	}
	private void populateAverageWeeklySpend() throws IllegalArgumentException	{
		try {
			financeTrackingService.retrieveAverageWeeklySpend(new Date(), false, 
					new AsyncCallback<Double>()	{

						@Override
						public void onFailure(Throwable caught) {
							averageWeeklySpend.setText("FAILURE");
						}

						@Override
						public void onSuccess(Double result) {
							averageWeeklySpend.setText(convertDoubleToString(result));
						}
					});
		} catch (SecurityAccessDeniedException e) {
			averageWeeklySpend.setText("FAILURE");
		}
	}
	
	private void populateForecastSavingsThisMonth() throws IllegalArgumentException	{
		try {
			financeTrackingService.retrieveForecastSavingsThisMonth(new Date(), true, 
					new AsyncCallback<Double>()	{

						@Override
						public void onFailure(Throwable caught) {
							forecastSavingThisMonth.setText("FAILURE");
						}

						@Override
						public void onSuccess(Double result) {
							forecastSavingThisMonth.setText(convertDoubleToString(result));
						}
					});
		} catch (SecurityAccessDeniedException e) {
			forecastSavingThisMonth.setText("FAILURE");
		}
	}
	
	private void populateWeekNumber() throws IllegalArgumentException	{
		try {
			financeTrackingService.retrieveWeekNumber(new Date(), 
					new AsyncCallback<String>()	{

						@Override
						public void onFailure(Throwable caught) {
							weekNumber.setText("FAILURE");
						}

						@Override
						public void onSuccess(String result) {
							weekNumber.setText(result);
						}
					});
		} catch (SecurityAccessDeniedException e) {
			weekNumber.setText("FAILURE");
		}
	}
	
	/*
	private void populateFinanceStatsFields()	{
		financeTrackingService.retrieveFinanceStats(
			new AsyncCallback<FinanceStatsDTO>()	{

				@Override
				public void onFailure(Throwable caught) {
					totalSpentThisWeekLabel.setText("FAILURE");
				}

				@Override
				public void onSuccess(FinanceStatsDTO result) {
					averageWeeklySpentThisMonthLabel.setText(result.getAverageWeeklySpentThisMonth().toString());
					weekNumberLabel.setText(result.getWeekNumber().toString());
					expectedSavingThisMonthLabel.setText(result.getExpectedSavingThisMonth().toString());
					spentThisMonthLabel.setText(result.getSpentThisMonth().toString());
					totalRemainingThisMonthLabel.setText(result.getTotalRemainingThisMonth().toString());
				}
		});
	}	*/
	private void populateTotalSavings()	{
		assetTrackingService.retrieveTotalAssetAmount(			
				new AsyncCallback<String>()	{

				@Override
				public void onFailure(Throwable caught) {
					totalSavingsLabel.setText("FAILURE");
				}

				@Override
				public void onSuccess(String result) {
					totalSavingsLabel.setText(convertStringDoubleToString(result));
				}
		});

	}
	
	private void populateForecastForSavingsAtEndOfNextQuarter()	{
		assetTrackingService.retrieveForecastedEndOfNextQuarterTotalAssets(true, new Date(), 
				new AsyncCallback<String>() {
					@Override
					public void onFailure(Throwable caught) {
						forecastSavingsAtEndOfNextQuarterLabel.setText("FAILURE");
					}

					@Override
					public void onSuccess(String result) {
						forecastSavingsAtEndOfNextQuarterLabel.setText(convertStringDoubleToString(result));
					}
				});
	}
	
	private void populateForecastUnemployedForSavingsAtEndOfNextQuarter()	{
		assetTrackingService.retrieveForecastedEndOfNextQuarterTotalAssets(false, new Date(), 
				new AsyncCallback<String>() {
					@Override
					public void onFailure(Throwable caught) {
						forecastUnemployedSavingsAtEndOfNextQuarterLabel.setText("FAILURE");
					}

					@Override
					public void onSuccess(String result) {
						forecastUnemployedSavingsAtEndOfNextQuarterLabel.setText(convertStringDoubleToString(result));
					}
				});
	}
	private void populateEndOfNextQuarter()	{
		assetTrackingService.retrieveEndOfNextQuarter(new Date(), 
				new AsyncCallback<String>() {
					@Override
					public void onFailure(Throwable caught) {
						endOfNextQuarterLabel.setText("FAILURE");
					}

					@Override
					public void onSuccess(String result) {
						endOfNextQuarterLabel.setText(result);
					}
				});
	}

	public String convertStringDoubleToString(String inValue)	{
		Double doubleValue = Double.parseDouble(inValue);
		return convertDoubleToString(doubleValue);
	}
	public String convertDoubleToString(Double inValue){
		String stringValue = inValue.toString();
		int positionOfDot = stringValue.indexOf(".");
		int lengthOfNumberText = stringValue.length();
		if ((lengthOfNumberText - positionOfDot) > 3)
			lengthOfNumberText = positionOfDot+3;
		if (positionOfDot>0)
			stringValue = stringValue.substring(0, lengthOfNumberText);
		return stringValue;
	}	
	
	
	private void attachPageComponents()	{
		setLabelWidths();
		addRowThirteen();
		addRowFourteen();
		addRowFifteen();
		addRowSixteen();
		addRowSeventeen();
		addRowEighteen();
		addRowNineteen();
		addRowOne();
		addRowTwo();
		addRowThree();
		addRowFour();
		addRowFive();
		addRowSix();
		addRowSeven();
		addRowEight();
		addRowNine();
		addRowTen();
		addRowEleven();
		addRowTwelve();
		setHeight("100%");		
	}
	
	private void setLabelWidths()	{
		setWidthsRowOne();
		setWidthsRowTwo();
		setWidthsRowThree();
		setWidthsRowFour();
		setWidthsRowFive();
		setWidthsRowSix();
		setWidthsRowSeven();
		setWidthsRowEight();
		setWidthsRowNine();
		setWidthsRowTen();
		setWidthsRowEleven();
		setWidthsRowTwelve();
		setWidthsRowThirteen();
		setWidthsRowFourteen();
		setWidthsRowFifteen();
		setWidthsRowSixteen();
		setWidthsRowSeventeen();
		setWidthsRowEighteen();
		setWidthsRowNineteen();
	}
	
	private void addRowOne()	{
		rowOne.setSpacing(3);
		setCellHeight(totalSpendThisWeekTitleLabel, cellHeight);
		rowOne.add(totalSpendThisWeekTitleLabel);
		setCellHeight(totalSavingsTitleLabel, cellHeight);
		rowOne.add(totalSavingsTitleLabel);
		add(rowOne);
	}
	private void setWidthsRowOne()	{
//		transactionTypeListBox.setWidth(columnOneWidth);
		totalSpendThisWeekTitleLabel.setWidth(columnTwoWidth);
		totalSavingsTitleLabel.setWidth(columnThreeWidth);
	}
	
	
	private void addRowTwo()	{	
		rowTwo.setSpacing(3);
		setCellHeight(totalSpentThisWeekLabel, cellHeight);
		rowTwo.add(totalSpentThisWeekLabel);
		setCellHeight(totalSavingsLabel, cellHeight);
		rowTwo.add(totalSavingsLabel);
		add(rowTwo);

	}
	private void setWidthsRowTwo()	{
//		amountTextBox.setWidth(columnOneWidth);
		totalSpentThisWeekLabel.setWidth(columnTwoWidth);
		totalSavingsLabel.setWidth(columnThreeWidth);
	}
	
	
	private void addRowThree()	{
		rowThree.setSpacing(3);
		setCellHeight(totalSpendThisMonthTitleLabel, cellHeight);
		rowThree.add(totalSpendThisMonthTitleLabel);
		setCellHeight(totalSavingsEndOfNextQuarterUnEmployedTitleLabel, cellHeight);
		rowThree.add(totalSavingsEndOfNextQuarterUnEmployedTitleLabel);
		add(rowThree);
	}
	private void setWidthsRowThree()	{
//		descriptionTextBox.setWidth(columnOneWidth);
		totalSpendThisMonthTitleLabel.setWidth(columnTwoWidth);
		totalSavingsEndOfNextQuarterUnEmployedTitleLabel.setWidth(columnThreeWidth);
	}
	
	
	private void addRowFour()	{
		rowFour.setSpacing(3);
		rowFour.add(totalSpentThisMonthLabel);
		setCellHeight(totalSpentThisMonthLabel, Integer.toString(totalSpentThisMonthLabel.getElement().getOffsetHeight())+"px");
		rowFour.add(forecastUnemployedSavingsAtEndOfNextQuarterLabel);
		setCellHeight(forecastUnemployedSavingsAtEndOfNextQuarterLabel, Integer.toString(forecastUnemployedSavingsAtEndOfNextQuarterLabel.getElement().getOffsetHeight())+"px");
		add(rowFour);
	}
	private void setWidthsRowFour()	{
//		sendButton.setWidth(columnOneWidth);
		totalSpentThisMonthLabel.setWidth(columnTwoWidth);
		forecastUnemployedSavingsAtEndOfNextQuarterLabel.setWidth(columnThreeWidth);
	}
	
	
	private void addRowFive()	{
		rowFive.setSpacing(3);		
		rowFive.add(totalSpendThisWeekForecastTitleLabel);
		setCellHeight(totalSpendThisWeekForecastTitleLabel, Integer.toString(totalSpendThisWeekForecastTitleLabel.getElement().getOffsetHeight())+"px");
		rowFive.add(totalSavingsEndOfNextQuarterEmployed);
		setCellHeight(totalSavingsEndOfNextQuarterEmployed, Integer.toString(totalSavingsEndOfNextQuarterEmployed.getElement().getOffsetHeight())+"px");
		add(rowFive);
	}
	private void setWidthsRowFive()	{
//		yearlyExpenseCheckBox.setWidth(columnOneWidth);
		totalSpendThisWeekForecastTitleLabel.setWidth(columnTwoWidth);
		totalSavingsEndOfNextQuarterEmployed.setWidth(columnThreeWidth);
	}
	
	
	private void addRowSix()	{
		rowSix.setSpacing(3);
		rowSix.add(forecastSpendThisWeek);
		setCellHeight(forecastSpendThisWeek, Integer.toString(forecastSpendThisWeek.getElement().getOffsetHeight())+"px");
		rowSix.add(forecastSavingsAtEndOfNextQuarterLabel);
		setCellHeight(forecastSavingsAtEndOfNextQuarterLabel, Integer.toString(forecastSavingsAtEndOfNextQuarterLabel.getElement().getOffsetHeight())+"px");
		add(rowSix);
	}
	private void setWidthsRowSix()	{
//		labelRowSixBlank.setWidth(columnOneWidth);
		forecastSpendThisWeek.setWidth(columnTwoWidth);
		forecastSavingsAtEndOfNextQuarterLabel.setWidth(columnThreeWidth);
	}
	
	
	private void addRowSeven()	{
		rowSeven.setSpacing(3);
		rowSeven.add(totalSpendThisMonthForecastTitleLabel);
		setCellHeight(totalSpendThisMonthForecastTitleLabel, Integer.toString(totalSpendThisMonthForecastTitleLabel.getElement().getOffsetHeight())+"px");
		rowSeven.add(endOfNextQuarterTitleLabel);
		setCellHeight(endOfNextQuarterTitleLabel, Integer.toString(endOfNextQuarterTitleLabel.getElement().getOffsetHeight())+"px");
		add(rowSeven);
	}
	private void setWidthsRowSeven()	{
//		labelRowSevenBlank.setWidth(columnOneWidth);
		totalSpendThisMonthForecastTitleLabel.setWidth(columnTwoWidth);
		endOfNextQuarterTitleLabel.setWidth(columnThreeWidth);
	}
	
	
	private void addRowEight()	{
		rowEight.setSpacing(3);
		rowEight.add(forecastSpendThisMonth);
		setCellHeight(forecastSpendThisMonth, Integer.toString(forecastSpendThisMonth.getElement().getOffsetHeight())+"px");
		rowEight.add(endOfNextQuarterLabel);
		setCellHeight(endOfNextQuarterLabel, Integer.toString(endOfNextQuarterLabel.getElement().getOffsetHeight())+"px");
		add(rowEight);
	}
	private void setWidthsRowEight()	{
//		labelRowEightBlank.setWidth(columnOneWidth);
		forecastSpendThisMonth.setWidth(columnTwoWidth);
		endOfNextQuarterLabel.setWidth(columnThreeWidth);
	}
	
	
	private void addRowNine()	{
		rowNine.setSpacing(3);
		rowNine.add(averageWeeklySpendTitleLabel);
		setCellHeight(averageWeeklySpendTitleLabel, Integer.toString(averageWeeklySpendTitleLabel.getElement().getOffsetHeight())+"px");
		add(rowNine);
	}
	private void setWidthsRowNine()	{
//		labelRowNineBlank.setWidth(columnOneWidth);
		averageWeeklySpendTitleLabel.setWidth(columnTwoWidth);
		
	}
	
	
	private void addRowTen()	{
		rowTen.setSpacing(3);
		rowTen.add(averageWeeklySpend);
		setCellHeight(averageWeeklySpend, Integer.toString(averageWeeklySpend.getElement().getOffsetHeight())+"px");
		add(rowTen);
	}
	private void setWidthsRowTen()	{
//		labelRowTenBlank.setWidth(columnOneWidth);
		averageWeeklySpend.setWidth(columnTwoWidth);
	}
	
	
	private void addRowEleven()	{
		rowEleven.setSpacing(3);
		rowEleven.add(forecastSavingsThisMonthTitleLabel);
		setCellHeight(forecastSavingsThisMonthTitleLabel, Integer.toString(forecastSavingsThisMonthTitleLabel.getElement().getOffsetHeight())+"px");
		add(rowEleven);
	}
	private void setWidthsRowEleven()	{
//		labelRowElevenBlank.setWidth(columnOneWidth);
		forecastSavingsThisMonthTitleLabel.setWidth(columnTwoWidth);
	}
	
	
	private void addRowTwelve()	{
		rowTwelve.setSpacing(3);
		rowTwelve.add(forecastSavingThisMonth);
		setCellHeight(forecastSavingThisMonth, Integer.toString(forecastSavingThisMonth.getElement().getOffsetHeight())+"px");
		add(rowTwelve);
	}
	private void setWidthsRowTwelve()	{
//		labelRowTwelveBlank.setWidth(columnOneWidth);
		forecastSavingThisMonth.setWidth(columnTwoWidth);
	}
	
	
	private void addRowThirteen()	{
		rowThirteen.setSpacing(3);
		rowThirteen.add(weekNumberTitleLabel);
		setCellHeight(weekNumberTitleLabel, Integer.toString(weekNumberTitleLabel.getElement().getOffsetHeight())+"px");
		add(rowThirteen);
	}
	private void setWidthsRowThirteen()	{
//		labelRowThirteenBlank.setWidth(columnOneWidth);
		weekNumberTitleLabel.setWidth(columnTwoWidth);
	}
	
	
	private void addRowFourteen()	{
		rowFourteen.setSpacing(3);
		rowFourteen.add(weekNumber);
		setCellHeight(weekNumber, Integer.toString(weekNumber.getElement().getOffsetHeight())+"px");
		add(rowFourteen);
	}		
	private void setWidthsRowFourteen()	{
//		labelRowFourteenBlank.setWidth(columnOneWidth);
		weekNumber.setWidth(columnTwoWidth);
	}
	
	private void addRowFifteen()	{
		rowFifteen.setSpacing(3);
		rowFifteen.add(transactionTypeListBox);
		add(rowFifteen);
	}
	private void setWidthsRowFifteen()	{
	}
	private void addRowSixteen()	{
		rowSixteen.setSpacing(3);
		rowSixteen.add(amountTextBox);
		add(rowSixteen);
	}
	private void setWidthsRowSixteen()	{
	}
	private void addRowSeventeen()	{
		rowSeventeen.setSpacing(3);
		rowSeventeen.add(descriptionTextBox);
		add(rowSeventeen);
	}
	private void setWidthsRowSeventeen()	{
		
	}
	private void addRowEighteen()	{
		rowEighteen.setSpacing(3);
		rowEighteen.add(sendButton);
		add(rowEighteen);
	}
	private void setWidthsRowEighteen()	{
		
	}
	private void addRowNineteen()	{
		rowNineteen.setSpacing(3);
		rowNineteen.add(yearlyExpenseCheckBox);
		add(rowNineteen);
	}
	private void setWidthsRowNineteen()	{
		
	}
	
	
	
	private void populateDynamicTypes()	{
		transactionTypeListBox.addItem("------");
//		transactionTypeListBox.addItem("Weekly Shop");
//		transactionTypeListBox.addItem("Breakfast");
//		transactionTypeListBox.addItem("Lunch");		
//		transactionTypeListBox.addItem("Dinner");
//		transactionTypeListBox.addItem("Coffee");
//		transactionTypeListBox.addItem("Date");
//		transactionTypeListBox.addItem("Social");		
//		transactionTypeListBox.addItem("Petrol");		
//		transactionTypeListBox.addItem("Other");		
		
		try {
			financeTrackingService.retrieveDynamicTypes("TransactionType", new AsyncCallback<List<DynamicTypeDTO>>()  {

				@Override
				public void onFailure(Throwable caught) {
					transactionTypeListBox.addItem("Failed!!!");

				}

				@Override
				public void onSuccess(List<DynamicTypeDTO> result) {
					for (DynamicTypeDTO dto:result)	{
						transactionTypeListBox.addItem(dto.getDynamicType());
					}
					
				} });
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityAccessDeniedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	private void populatePageComponents()	{
		nameField.setText("GWT User1");
		
		populateDynamicTypes();
		
		amountTextBox.setText("0.0");
		descriptionTextBox.setText("");
		sendButton.addStyleName("sendButton");
		nameField.setFocus(true);
		nameField.selectAll();
		yearlyExpenseCheckBox.setText("Yearly Expense");
		yearlyExpenseCheckBox.setValue(false);
	}
	
	private void createDialogBox()	{
		// Create the popup dialog box
		dialogBox.setText("Remote Procedure Call");
		dialogBox.setAnimationEnabled(true);
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
		dialogVPanel.add(textToServerLabel);
		dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
		dialogVPanel.add(serverResponseLabel);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);

		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				sendButton.setEnabled(true);
				sendButton.setFocus(true);
			}
		});
	}
	
	private void createHandlers()	{
		// Create a handler for the sendButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler, ChangeHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				sendTrackingEventToServer();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					sendTrackingEventToServer();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a response.
			 */
			private void sendTrackingEventToServer() {
				// First, we validate the input.
				errorLabel.setText("");
				String transactionType = transactionTypeListBox.getItemText(transactionTypeListBox.getSelectedIndex());
				Double amount = new Double(amountTextBox.getText());
				String description = descriptionTextBox.getText();
				Boolean yearlyExpense = yearlyExpenseCheckBox.getValue();

				TransactionDTO dto = new TransactionDTO(null, new Date(), transactionType, amount, description, yearlyExpense);
								
				sendButton.setEnabled(false);
				financeTrackingService.insertTransaction(dto,
						new AsyncCallback<String>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								dialogBox.setText("Remote Procedure Call - Failure");
								serverResponseLabel.addStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(SERVER_ERROR);
								dialogBox.center();
								dialogBox.setPopupPosition(0, 0);
								closeButton.setFocus(true);
							}

							public void onSuccess(String result) {
								dialogBox.setText("Remote Procedure Call");
								serverResponseLabel.removeStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(result);
								dialogBox.center();
								dialogBox.setPopupPosition(0, 0);
								closeButton.setFocus(true);
								amountTextBox.setText("0.0");
								descriptionTextBox.setText("");
								populateFinanceStatsFields();
							}
						});
			}
			
			@Override
			public void onChange(ChangeEvent event) {
				amountTextBox.setText("0.0");
				descriptionTextBox.setText("");
			}
		}
		
		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		sendButton.addClickHandler(handler);
		nameField.addKeyUpHandler(handler);
		transactionTypeListBox.addChangeHandler(handler);
	}	
}
